
keywords = ['exemptions','exempt','except']

def first_occurence(segments,text):
    start_pos_of_segments = list(map(lambda s : text.index(s),segments))
    zip_start_pos_with_segments = list(zip(start_pos_of_segments,segments))
    zip_start_pos_with_segments.sort(key=lambda start_and_segment : start_and_segment[0])
    return list(zip_start_pos_with_segments)


def join(sorted_segments,text):
    if sorted_segments is None or len(sorted_segments)==0:
        return ''
    final = [sorted_segments[0][1]]
    l = len(sorted_segments[0][1])
    current_index = sorted_segments[0][0] + l-1
    for s in sorted_segments[1:]:
        print(s," ,current_index="+str(current_index)," ,s[0]="+str(s[0])," ,l="+str(l), text[current_index])
        if s[0] > current_index:
            final.append('\n '+s[1]) # this is the start of a new word, so the added space
            current_index = s[0] + len(s[1])-1
        elif current_index >= s[0] and current_index < s[0] + len(s[1])-1:
            final.append(text[current_index+1:s[0] + len(s[1])])
            current_index = s[0] + len(s[1])-1
    return ''.join(final)


def filter(text,paragraph,using_keywords = True):
    pass

s = "this is a test. the join should work correctly, will check this with assertions"

# first segment consumes all the others
slices = ["this is a test. the join should work correctly","this is a test","test. the join should work correctly","work correctly"]
result = (join(first_occurence(slices,s),s))
# print (result)
assert result == slices[0]


# first segment consumes all the others except the last one
slices = ["this is a test. the join should work","this is a test","work correctly"]
result = (join(first_occurence(slices,s),s))
# print(result)
assert result == "this is a test. the join should work correctly"


# first segment consumes all the others except the last one (which is completely different)
slices = ["this is a test. the join","the join should", "work correctly","with assertions","test. the join should work correctly, will check this with assertions"]
result = (join(first_occurence(slices,s),s))
# print(result)
assert result == "this is a test. the join should work correctly, will check this with assertions"
